const TIMEOUTS = {
  bootstrap: {
    milliseconds: 3000,
    rejectWhenTimeout: false
  },
  mount: {
    milliseconds: 3000,
    rejectWhenTimeout: false
  },
  unmount: {
    milliseconds: 3000,
    rejectWhenTimeout: false
  }
};

export function reasonableTime(promise, description, timeouts) {
  return new Promise((resolve, reject) => {
    let finished = false;

    promise.then(data => {
      finished = true;
      resolve(data);
    }).catch(e => {
      finished = true;
      reject(e);
    });


    setTimeout(() => maybeTimeout(), timeouts.milliseconds);

    function maybeTimeout() {
      if (finished) {
        return;
      }
      let error = `${description} did not resolve or reject for ${timeouts.milliseconds} milliseconds`;
      if (timeouts.rejectWhenTimeout) {
        reject(new Error(error));
      } else {
        console.error(error);
      }
    }

  });
}

export function ensureTimeout(timeouts = {}){
  return {
    ...TIMEOUTS,
    ...timeouts
  }
}
